home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
178_01
/
tvx_io.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-01-16
|
42KB
|
1,870 lines
/* ---------------------------- tvx_io.c ------------------------------- */
#include "tvx_defs.ic"
#include "tvx_glbl.ic"
#define SWITCH '-'
#define FILESEP '.'
#ifdef MSDOS
#define TEMPEXT ".$$1" /* name of temporary file */
#define BACKEXT ".BAK" /* name of backup file */
#endif
#ifdef OSCPM
#define TEMPEXT ".$$1" /* name of temporary file */
#define BACKEXT ".BAK" /* name of backup file */
#endif
#ifdef GEMDOS
#define TEMPEXT ".Z1X" /* name of temporary file */
#define BACKEXT ".BAK" /* name of backup file */
#endif
#ifdef UNIX
#define BACKEXT ".B" /* name of backup file */
#endif
FILE *fopen();
/* local globals (used by tv terminal driver section) */
static int linptr; /* common "linot" */
static char linout[242];
static char stemp[FNAMESIZE+1];
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
FILE IO section
File handling algorithm:
The original name specified on command line is called orig_file.
It will remain untouched throughout the editing session. At the
very end (normal exit), it will be renamed to the ".BAK" name.
source_file is the current name of the file with source. It will
orignally be the same as orig_file, but may change to a generated
scratch name depending on the operating system. source_file is
always the lastest fully written version of the file (like after
file beginning, for example).
work_file is the output file. On normal exit, this is the
file renamed to dest_file. On buffer beginning, it will be
moved to source_file, possibly after renameing.
dest_file is the ultimate destination file. This name is not
actually used until the final rename on normal exit. It is
checked to be sure it is a valid name to be opened, however.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* =============================>>> ABORT <<<============================= */
abort()
{ /* abort - close files, abort operation */
char rply[4];
tvclr();
ask("Abort, are you sure? ",rply,1);
if (clower(rply[0]) != 'y')
{
verify(1);
return;
}
abort2();
}
/* =============================>>> ABORT2 <<<============================= */
abort2()
{
clobak();
tvclr();
if (!newfil)
fclose(infile);
if (!rdonly)
fclose(outfile);
if (strcmp(orig_file,source_file) != 0)
{
prompt("File begin used, intermediate edits in: ");
remark(source_file);
}
unlink(work_file); /* delete the work file */
reset();
quit();
}
/* =============================>>> FBEG <<<============================= */
int fbeg()
{ /* fbeg - go back to file top */
SLOW int fbegv;
if (rdonly)
{
tverrb("Can't: R/O"); /* can't do this for read only access */
return (FALSE);
}
for (wtpage(1) ; rdpage() ; wtpage(1) ) /* write out rest */
;
if ((! newfil))
{
fclose(infile); /* close source_file */
}
if (usecz)
fputc(ENDFILE,outfile);
fclose(outfile); /* close work_file */
/* files closed now, re-open */
newfil = FALSE; /* not a new file any more */
strcpy(source_file,work_file); /* new source file */
temp_name(work_file,FALSE); /* make a new temporary name */
if (!(infile = fopen(source_file,FILEREAD)))
goto l900;
else
ineof = FALSE;
unlink(work_file); /* get rid of previous copies */
if (!(outfile = fopen(work_file,FILEWRITE)))
{
goto l900;
}
fbegv=rdpage(); /* read in new buffer */
newscr();
return (fbegv);
l900: tverrb("Error re-opening");
return (FALSE);
}
/* =============================>>> FILE_EXIT <<<============================= */
file_exit()
{ /* close the input and output files, rename */
SLOW int i;
if (!newfil) /* don't close input if new file */
{
fclose(infile);
}
while (!rdonly && !*dest_file)
{
remark("No name for output file has been specified.");
prompt("Enter new name for output file: ");
reply(dest_file,FNAMESIZE);
}
if (!rdonly) /* don't close output if read only access */
{
if (usecz)
fputc(ENDFILE,outfile);
set_mode(outfile); /* set output mode if can */
fclose(outfile);
/* orig_file has the name to be renamed to .bak
work_file has the file name we want to be dest_name
*/
if (strcmp(orig_file,dest_file) == 0) /* make backup version */
{
strcpy(stemp,orig_file);
#ifndef COMMA_BAK
if ((i = rindex(stemp,FILESEP)) > 0) /* see if extenstion */
scopy(BACKEXT,0,stemp,i); /* make .bak */
else
{
scopy(BACKEXT,0,stemp,strlen(stemp)); /* just add on */
}
#else
i = rindex(orig_file,'/')+1;
scopy(".,",0,stemp,i);
scopy(orig_file,i,stemp,strlen(stemp));
#endif
unlink(stemp); /* delete previous generation */
ren_file(orig_file,stemp); /* rename the file */
if (!makebackup) /* don't want to keep old one */
unlink(stemp); /* delete it if don't want backup file */
}
if (strcmp(orig_file,source_file) != 0) /* delete intermediate file */
unlink(source_file);
while (infile = fopen(dest_file,FILEREAD)) /* output exists? */
{
fclose(infile);
prompt("Output file "); prompt(dest_file);
prompt(" already exists. Overwrite it? (y/n) ");
ureply(stemp,1);
if (*stemp == 'Y')
{
unlink(dest_file);
break;
}
prompt("Enter new name for output file: ");
reply(dest_file,FNAMESIZE);
}
ren_file(work_file,dest_file); /* finally, rename last file */
}
}
/* =============================>>> FOPENX <<<============================= */
fopenx(argc,argv)
int argc;
char *argv[];
{ /* open the input file
This routine picks up file name from the user, creates a backup
version in some appropriate manner, and opens the file for input
and output. */
SLOW int iswval, iswbeg, argnum, set_ttymode;
SLOW char ch;
char rply[4];
usebak = logdef; /* if useing backup log file */
ttymode = FALSE; /* not in tty mode, so io ok */
ttynext = 1000; /* force read */
if (argc <= 1)
{
remark("Usage: tvx filename [-b -i -l -o=f -r -s -t -w -# {-z -c=f}]");
#ifdef FULLHELP
remark("");
prompt(" Options: "); remark(VERSION);
remark(" -[no]b : backup file -[no]i : autoindent");
remark(" -[no]l : make command log file");
remark(" -o=outputfile -r : read only");
remark(" -s : big save buff -[no]w : word processing mode");
remark(" -t : tty edit mode -# : set virtual window lines to #");
#ifdef MSDOS
remark(" -[no]z : use control-z for end of file");
#endif
#ifdef CONFIGFILE
#ifdef MSDOS
remark(" -c=configfile -c : use /bin/config.tvx");
#endif
#ifdef GEMDOS
remark(" -c=configfile -c : use /bin/config.tvx");
#endif
#ifdef OSCPM
remark(" -c=configfile -c : use A:config.tvx");
#endif
#endif
#ifdef UNIX
remark(" {options not available for unix}");
#endif
#endif
remark("");
reset();
quit();
}
newfil= /* assume opening an old file */
rdonly = FALSE; /* assume not read only */
makebackup = MAKE_BACKUP; /* default value for make a backup */
blimit = BUFFLIMIT;
for (argnum = 1 ; argnum < argc ; ++argnum)
{
strcpy(stemp,argv[argnum]); /* pick up the file name or switch */
REDO:
if (stemp[0] == SWITCH) /* switch in form "/R filename" only */
{
iswbeg=1; /* start at 1 */
iswval = TRUE;
if (clower(stemp[1]) == 'n' && clower(stemp[2]) == 'o')
{
iswval = FALSE ; iswbeg = 3 ;
}
ch = clower(stemp[iswbeg]); /* get the char */
if (ch == 'r') /* read only access */
rdonly=iswval;
else if (ch == 'i') /* auto indent */
autoin = iswval;
else if (ch == 'w') /* word processing mode */
{
if (iswval)
w